package com.renfei.ffmpegvideoplayer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {


    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.MODIFY_AUDIO_SETTINGS

    };
    private static final int PERMISSIONS_READ_STORAGE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.sample_text).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkReadStoragePermission();


            }
        });
    }


    private void checkReadStoragePermission() {
        if (EasyPermissions.hasPermissions(this, PERMISSIONS)) {
            gotoDecodeAudioPage();
        } else {
            applyPermission();
        }

    }

    private void gotoDecodeAudioPage() {
        startActivity(new Intent(MainActivity.this, VideoPlayActivity.class));
    }


    private void applyPermission() {
        EasyPermissions.requestPermissions(this, "申请读取存储权限,", PERMISSIONS_READ_STORAGE, PERMISSIONS);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PERMISSIONS_READ_STORAGE) {
            gotoDecodeAudioPage();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PERMISSIONS_READ_STORAGE) {
            Toast.makeText(this,"未获取到权限",Toast.LENGTH_SHORT).show();
        }
    }
}
